/**
 * Created by zhuangqiuyu on 2017/4/20.
 */
(function () {

    var analyze_url;
    var params = {};
    //Document对象数据
    if (document) {
        params.domain = document.domain || '';
        params.url = document.URL || '';
        params.title = document.title || '';
        params.referrer = document.referrer || '';
    }
    //Window对象数据
    if (window && window.screen) {
        params.sh = window.screen.height || 0;
        params.sw = window.screen.width || 0;
        params.cd = window.screen.colorDepth || 0;
    }
    //navigator对象数据
    if (navigator) {
        params.lang = navigator.language || '';
    }
    params.client = 'web app';
    params.id = localStorage.getItem('user_id');
    //解析_maq配置
    // if (_maq) {
    //     for (var i in _maq) {
    //         switch (_maq[i][0]) {
    //             case '_setClient':
    //                 params.client = _maq[i][1];
    //                 break;
    //             default:
    //                 break;
    //         }
    //     }
    // }

    function send() {
        if (!params.id) {
            return
        }

        //拼接参数串
        var args = '';
        for (var i in params) {
            if (args != '') {
                args += '&';
            }
            args += i + '=' + encodeURIComponent(params[i]);
        }

        //通过Image对象请求后端脚本
        var img = new Image(1, 1);
        img.src = analyze_url + '?' + args;
    }

    function loadingAnalyze(url) {
        analyze_url = url;
        send();
    }

    // function on(action, selector, callback) {
    //     document.addEventListener(action, function (e) {
    //         if (e.target.className.match(selector.replace('.', ' '))) {
    //             console.log("Click Event");
    //             callback(e.target);
    //         }
    //     })
    // }
    //
    //
    // on('click', '.download', downloadRequest);

    function downloadRequest(target) {
        params.click = 'download';
        params.download_url = target.href;
        params.game_code = target.getAttribute('data-code');
        params.id = target.getAttribute('data-id');

        send();
    }

    function loginRequest(id) {
        params.id = id;
        send();
    }


    window.onbeforeunload = function (e) {
        params.click = 'close';
        send();
        setTimeout(function () {
        }, 1000);  //就是加入这一行！
    };

    window.downloadRequest = downloadRequest;
    window.loginRequest = loginRequest;
    window.loadingAnalyze = loadingAnalyze;
})();
